package com.example.carrierapp.ui.main.dispatch.detail.history;

import android.content.Context;

import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.bean.RowsBean;
import com.example.carrierapp.bean.dispatch.DispatchHistoryBean;
import com.example.carrierapp.net.ApiMiddleHelper;
import com.example.carrierapp.net.listener.DataCallBackListener;
import com.example.carrierapp.util.LogUtil;
import com.example.carrierapp.viewmodel.itemmodel.ItemDispatchHistoryModel;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;

public class DispatchHistoryPresenter extends BasePresenter<DispatchHistoryContract.View> implements DispatchHistoryContract.Presenter {
    private Context mContext;

    public DispatchHistoryPresenter(Context context) {
        mContext = context;
    }

    @Override
    public void start() {

    }

    @Override
    public void getDeliveryHistoryDetail(int id, String filter, int skipCount) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("id", id + "");
        biddingMap.put("maxResultCount", 10 + "");
        biddingMap.put("skipCount", skipCount + "");
        biddingMap.put("filter", filter);
        Flowable<JsonObject> flowable = apiStrores.getDispatcHistoryhList(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, new DataCallBackListener() {

            @Override
            public void onSuccess(String json) {
                LogUtil.d("getDeliveryHistoryDetail-onSuccess-" + json);

                RowsBean<ItemDispatchHistoryModel> bean = new Gson().fromJson(json, new TypeToken<RowsBean<ItemDispatchHistoryModel>>() {
                }.getType());
                if (getView() != null) {
                getView().getDeliveryHistoryDetailSuccess(bean);

                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("getDeliveryHistoryDetail-onFailure-" + msg);
                if (getView() != null) {
                getView().getDeliveryHistoryDetailFailde(msg);

                }
            }
        });
    }

    @Override
    public void dispatchAgain(Integer carId, Integer waybillId, String weight, Integer driverId) {
        Map<String, String> biddingMap = new HashMap<>();

        biddingMap.put("waybillId", waybillId + "");
        biddingMap.put("driverMainId", driverId + "");
        biddingMap.put("carId", carId + "");
        biddingMap.put("weight", weight + "");
        Flowable<JsonObject> flowable = apiStrores.dispatchAgain(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, new DataCallBackListener() {

            @Override
            public void onSuccess(String json) {
                LogUtil.d("dispatchAgain-onSuccess-" + json);
                if (getView() != null) {
                getView().dispatchAgainSuccess(json);

                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("dispatchAgain-onFailure-" + msg);
                getView().dispatchAgainDetailFailde(msg);
            }
        });
    }

    @Override
    public void dispatchModify(Integer driverId, Integer carId, Integer waybillId, String expectedWeight, boolean isMultiple,
                               boolean isMultipleBak, String limitCount, String validityPeriod, Integer id) {
        Map<String, String> biddingMap = new HashMap<>();

        biddingMap.put("waybillId", waybillId + "");
        biddingMap.put("driverMainId", driverId + "");
        biddingMap.put("carId", carId + "");
        biddingMap.put("expectedWeight", expectedWeight + "");
        biddingMap.put("isMultiple", isMultiple + "");
        biddingMap.put("limitCount", limitCount + "");
        biddingMap.put("validityPeriod", validityPeriod + "");
        biddingMap.put("id", id + "");
        biddingMap.put("isMultipleBak", isMultipleBak + "");
        Flowable<JsonObject> flowable = apiStrores.dispatchModify(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, new DataCallBackListener() {

            @Override
            public void onSuccess(String json) {
                LogUtil.d("dispatchModify-onSuccess-" + json);
                if (getView() != null) {
                getView().dispatchModifySuccess(json);

                }

            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("dispatchModify-onFailure-" + msg);
                if (getView() != null) {
                getView().dispatchModifyFailde(msg);

                }
            }
        });
    }
}
